package tc.crase.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JPanel;

import tc.common.registry.Obj;
import tc.common.registry.Ref;
import tc.common.registry.RegistrySys;
import tc.common.registry.Rview;
import tc.common.utility.TcCmd;

import tc.crase.cmds.CmdEx;
import tc.common.symbology.Style;
import tc.crase.sys.XdbEx;
import tc.crase.sys.XobjectEx;

public class ViewPairs extends JPanel implements Rview, MouseListener, AdjustmentListener {

	private Vector<Ref> refsLeft  = new Vector<Ref>();
	private Vector<Ref> refsRight = new Vector<Ref>();
	//JScrollBar scroller;
	public JPanel view;
	//int scrollIndex;
	
	public ViewPairs() {
	
		addMouseListener((MouseListener) this);
	
		RegistrySys.registry.views.add(this);
	
		/*view = new JPanel();
		view.setLayout(new BorderLayout());
		scroller = new JScrollBar(JScrollBar.VERTICAL, 30, 20, 0, 300);
		view.add(this, BorderLayout.CENTER);
		view.add(scroller, BorderLayout.EAST);
		addComponentListener(new Listener(this));
		scroller.addAdjustmentListener(this);*/
		
		//update();
	}
	
	public void add( XobjectEx o1, XobjectEx o2 ) {
		refsLeft.add(new Ref(o1.getObj(), this));
		refsRight.add(new Ref(o2.getObj(), this));
	}
	
	public void adjustmentValueChanged(AdjustmentEvent e) {
	/*	scrollIndex = e.getValue();
		repaint();*/
	}
	
	/*private void update() {
		int lines = getHeight() / lineHeight() - 1;
		if (lines <= 0)
			lines = 1;
		if (refs.size() <= lines) {
			scroller.setVisible(false);
			scrollIndex = 0;
		return;
		}
	
		int maxScrollIndex = refs.size() - lines;
		if (scrollIndex > maxScrollIndex)
			scrollIndex = maxScrollIndex;
		scroller.setMinimum(0);
		scroller.setMaximum(maxScrollIndex);
		scroller.setValue(scrollIndex);
	
		scroller.setVisible(true);
	
		repaint();
	}*/
	
	public int lineHeight() {
		return 22;
	}
	
	public void paint(Graphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
	
		int lines = getHeight() / lineHeight();
	
		//for (int i = scrollIndex; i < refs.size(); i++) {
		for (int i = 0; i < refsLeft.size(); i++) {
			int linei = i;// - scrollIndex;
			if (linei > lines) break;
	
			int Y = (linei + 1) * lineHeight();
			
			XobjectEx.ObjEx obj1 = (XobjectEx.ObjEx) refsLeft.get(i).original;
			paintObject(g, obj1.getObject(), 0, Y);
			
			XobjectEx.ObjEx obj2 = (XobjectEx.ObjEx) refsRight.get(i).original;
			paintObject(g, obj2.getObject(), getWidth()/2, Y);
		}
    }
	
	public void paintObject(Graphics g, XobjectEx xobj, int X, int Y) {
		Style style = null;
	
		XdbEx xdbex = (XdbEx) xobj.getDb();
		style = (Style) xdbex.metadata.legend.styles.get(xobj.getType());
	
		if (xobj.getSelected()) {
			ViewRoot.styleSelected.drawIcon(g, X, Y - lineHeight() + 4,
					                  getWidth(), lineHeight());
		}
	
		if (style != null) {
			style.drawIcon(g, X+5, Y - 12, 14, 14);
		}
	
		g.setColor(Color.BLACK);
	
		String item = xobj.getName() + " [" + xobj.getId() + "]";
		g.drawString(item, X+26, Y);
	}
	
	public void removeNotify() {
	super.removeNotify();
	}
	
	boolean dirty = false;
	
	public boolean getDirty() {
	return dirty;
	};
	
	public void setDirty(boolean value) {
	dirty = value;
	};
	
	public void checkPresence() {
	for (int i = 0; i < RegistrySys.registry.changed.size(); i++) {
		/*Obj obj = RegistrySys.registry.changed.get(i);
		if (obj.state != Obj.State.NEW)
			continue;
		if (obj instanceof XobjectEx.ObjEx == false)
			continue;
		XobjectEx.ObjEx xobj = (XobjectEx.ObjEx)obj;
		if (xobj.getObject().pass(filterType, filterContentRegexp) == false)
			continue;
		refs.add(new Ref(xobj, this));
		dirty = true;*/
		}
	}
	
	public void updateRview() {
		//update();
		//repaint();
	}
	
	public void onRefChanged(Ref ref) {
		/*if (ref.original.state == Obj.State.CHANGED) {
			dirty = true;
		}
		else if (ref.original.state == Obj.State.DELETED) {
			refs.remove(ref);
		}*/
	}
	
	public void mouseClicked(MouseEvent e) {
		if (TcCmd.activeCommand instanceof CmdEx == false) return;
		CmdEx cmdex = (CmdEx)TcCmd.activeCommand;
		
		if (e.getButton() == MouseEvent.BUTTON1) {
			Vector<Ref> refs = e.getPoint().x < getWidth()/2 ? refsLeft : refsRight;
			
			int index = e.getPoint().y / lineHeight();// + scrollIndex;
			if (index >= refs.size())
				return;
			Ref ref = refs.get(index);
			XobjectEx.ObjEx xobj = (XobjectEx.ObjEx) ref.original;
			cmdex.enterSelected(e.getPoint(), null, xobj.getObject());
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			cmdex.enterReset(e.getPoint(), null);
			return;
		}
	}
	
	public void mouseEntered(MouseEvent e) {
	}
	
	public void mouseExited(MouseEvent e) {
	}
	
	public void mousePressed(MouseEvent e) {
	}
	
	public void mouseReleased(MouseEvent e) {
	}
	
	class Listener implements ComponentListener {
			
		public void componentResized(ComponentEvent e) {
			//view.update();
		}
	
		public void componentHidden(ComponentEvent e) {
		}
	
		public void componentMoved(ComponentEvent e) {
		}
	
		public void componentShown(ComponentEvent e) {
		}
	}
}
